<template>
  <div class="parking-area-list">
    <div class="breadcrumb">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item to="Home" :replace="true">首页</el-breadcrumb-item>
        <el-breadcrumb-item>车位信息列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-form :inline="true" :model="searchParams">
      <el-form-item label="车位号">
        <el-input v-model="searchParams.areaNumber" placeholder="请输入车位号" maxlength="32"></el-input>
      </el-form-item>
      <el-form-item label="楼栋名称" prop="buildingName">
        <el-input v-model="searchParams.buildingName" placeholder="请选择楼栋名称" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findBuilding"></i>
        </el-input>
      </el-form-item>
      <el-form-item label="所在楼层">
        <el-input v-model="searchParams.floorNumber" placeholder="请输入所在楼层" maxlength="32"></el-input>
      </el-form-item>
      <el-form-item label="单元房号" prop="residentRoomNumber">
        <el-input v-model="searchParams.residentRoomNumber" placeholder="请选择单元房号" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findRoom"></i>
        </el-input>
      </el-form-item>
      <el-form-item label="车牌">
        <el-input v-model="searchParams.licensePlate" placeholder="请输入车牌" maxlength="32"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="searchParams.status" placeholder="请选择状态">
          <el-option label="临停" :value="30"></el-option>
          <el-option label="业主" :value="10"></el-option>
          <el-option label="月租" :value="20"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button type="primary" @click="onAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="pageData.list"
      border
      v-loading="pageData.loading"
      size="mini"
      header-cell-class-name="table-header-cell-font"
      cell-class-name="table-cell-font">
      <el-table-column prop="areaNumber" label="车位号"></el-table-column>
      <el-table-column prop="buildingName" label="楼栋名称"></el-table-column>
      <el-table-column prop="floorNumber" label="所在楼层"></el-table-column>
      <el-table-column prop="residentRoomNumber" label="单元房号"></el-table-column>
      <el-table-column prop="licensePlate" label="车牌"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="{ row }">
          <div v-if="row.status === 10">业主</div>
          <div v-if="row.status === 20">月租</div>
          <div v-if="row.status === 30">临停</div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template v-slot="{ row }">
          <el-button @click.native.prevent="editRow(row.id)" type="primary">编辑</el-button>
          <el-button @click.native.prevent="delRow(row.id)" type="danger" plain>删除</el-button>
          <el-button @click.native.prevent="detailRow(row.id)" type="primary" plain>详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination">
      <el-pagination
        @current-change="paginationChange"
        :current-page="pageData.current"
        :page-sizes="pageData.pageSizes"
        :page-size="pageData.size"
        background
        layout="total, prev, pager, next, jumper"
        :total="pageData.count">
      </el-pagination>
    </div>
    <el-dialog :visible.sync="buildingSelectorVisible" v-if="buildingSelectorVisible" title="楼栋信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth">
      <building-selector @building-selected-event="handleBuildingSelectedEvent">
      </building-selector>
    </el-dialog>
    <el-dialog :visible.sync="roomSelectorVisible" v-if="roomSelectorVisible" title="单元信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <room-selector @room-selected-event="handleRoomSelectedEvent">
      </room-selector>
    </el-dialog>
    <el-dialog :visible.sync="parkingAreaAddVisible" v-if="parkingAreaAddVisible" :title="parkingAreaAddTitle" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <parking-area-add :id="selectedParkingAreaId" @close-parking-area-add-event="handleCloseParkingAreaAddEvent" @reset-parking-area-add-event="handleResetParkingAreaAddEvent">
      </parking-area-add>
    </el-dialog>
    <el-dialog :visible.sync="parkingAreaViewVisible" v-if="parkingAreaViewVisible" title="车位信息详情" :top="dialogTop" :width="dialogWidth">
      <parking-area-view :id="selectedParkingAreaId" @close-parking-area-view-event="handleCloseParkingAreaViewEvent">
      </parking-area-view>
    </el-dialog>
  </div>
</template>

<script>
import parkingAreaApi from '@/api/parkingAreaApi'
import BuildingSelector from "@/views/building/BuildingSelector.vue";
import RoomSelector from "@/views/room/RoomSelector.vue";
import ParkingAreaAdd from "@/views/parkingArea/ParkingAreaAdd.vue"
import ParkingAreaView from "@/views/parkingArea/ParkingAreaView.vue"
import listQueryMixin from '@/mixins/listQueryMixin'
export default {
  name: 'ParkingAreaList',
  components: {BuildingSelector,RoomSelector,ParkingAreaAdd, ParkingAreaView},
  mixins: [
    listQueryMixin
  ],
  data () {
    return {
      searchParams: {
        areaNumber: '',
        buildingId: '',
        buildingName: '',
        floorNumber: '',
        residentRoomId: '',
        residentRoomNumber: '',
        licensePlate: '',
        status: ''
      },
      buildingSelectorVisible: false,
      roomSelectorVisible: false,
      parkingAreaAddVisible: false,
      parkingAreaAddTitle: '',
      parkingAreaViewVisible: false,
      selectedParkingAreaId: 0
    }
  },
  methods: {
    getPageData (p) {
      return parkingAreaApi.find(p)
    },
    onSearch () {
      this._getPageData()
    },
    onReset () {
      this.searchParams = {
        areaNumber: '',
        buildingId: '',
        buildingName: '',
        floorNumber: '',
        residentRoomId: '',
        residentRoomNumber: '',
        licensePlate: '',
        status: ''
      }
      this.pageData.pageIndex = 1
      this._getPageData()
    },
    onAdd () {
      this.selectedParkingAreaId = ''
      this.parkingAreaAddVisible = true
      this.parkingAreaAddTitle = '车位信息新增'
    },
    editRow (id) {
      this.selectedParkingAreaId = id
      this.parkingAreaAddVisible = true
      this.parkingAreaAddTitle = '车位信息编辑'
    },
    delRow (id) {
      if (!id) {
        return
      }
      this.$confirm('确定要删除吗?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        parkingAreaApi.del(id)
          .then((resp) => {
            if (resp && resp.code === 1) {
              this.$message.success('删除成功!')
              this.onSearch()
            } else {
              this.$message.error('删除失败:' + (resp.msg ? resp.msg : '原因未知'))
            }
          })
          .catch(error => {
            console.error('操作失败:', error)
          })
      }).catch(() => {})
    },
    detailRow (id) {
      this.selectedParkingAreaId = id
      this.parkingAreaViewVisible = true
    },
    findBuilding () {
      this.buildingSelectorVisible = true
    },
    handleBuildingSelectedEvent (selectedBuilding) {
      this.buildingSelectorVisible = false
      if (selectedBuilding && 'buildingId' in selectedBuilding && this.searchParams.buildingId !== selectedBuilding['buildingId']) {
        this.searchParams.buildingId = selectedBuilding['buildingId']
        this.searchParams.buildingName = selectedBuilding['buildingName']
      }
    },
    findRoom () {
      this.roomSelectorVisible = true
    },
    handleRoomSelectedEvent (selectedRoom) {
      this.roomSelectorVisible = false
      if (selectedRoom && 'roomId' in selectedRoom && this.searchParams.residentRoomId !== selectedRoom['roomId']) {
        this.searchParams.residentRoomId = selectedRoom['roomId']
        this.searchParams.residentRoomNumber = selectedRoom['roomNumber']
      }
    },
    handleCloseParkingAreaViewEvent () {
      this.parkingAreaViewVisible = false
    },
    handleResetParkingAreaAddEvent () {
      this.parkingAreaAddTitle = '车位信息新增'
    },
    handleCloseParkingAreaAddEvent (params) {
      if (params && params.search) {
        this.onSearch()
      }
      this.parkingAreaAddVisible = false
    }
  }
}
</script>

<style lang="scss">
</style>